#ifndef __SOCK_MY_
#define __SOCK_MY_
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>       
#include <sys/socket.h>
#include <cstring>
#include <unistd.h>
#include <cstring>
#include <cstdlib>
#include <iostream>
using namespace std;

class Sock
{
private:
    int _fd;
public:
    void Socket()
    {
        _fd = socket(AF_INET, SOCK_STREAM, 0);
        if(_fd < 0)
        {
           // perror("socket error");
            exit(-1);
        }
    }

    void Bind()
    {
        struct sockaddr_in local;
        local.sin_family = AF_INET;
        local.sin_addr.s_addr = INADDR_ANY;
        local.sin_port = htons(8085);

        if(bind(_fd, (struct sockaddr*)&local, sizeof(local)) < 0)
        {   
            perror("bind error");
            std::cout << "bind erorr" << std::endl;
            exit(-1);   
        }
    }

    void Listen()
    {
        if(listen(_fd, 10) < 0)
        {
            perror("listen error");
            exit(-1);
        }
    }

    int Accept()
    {
        int n = accept(_fd, nullptr, nullptr);
        if(n < 0)
        {
            perror("accept error");
            exit(-1);
        }

        return n;
    }

    void Connect(string ip, int port)
    {
        struct sockaddr_in server;
        server.sin_family = AF_INET;
        server.sin_port = htons(port);
        server.sin_addr.s_addr = inet_addr(ip.c_str());

        int n = connect(_fd, (struct sockaddr*)&server, sizeof(server));
        if(n  < 0)
        {
            perror("connect error");
            exit(-1);
        }
        std::cout << "connect sucess" << std::endl;
    }

    int Fd()
    {
        return _fd;
    }
};

#endif